﻿using System;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IisHost.UI
{
    public partial class RuntimeControl : UserControl
    {
        public void BindData()
        {
            lblMessage.Visible = false;
            lstState.DataSource = HostRuntime.Initialized 
                ? HostRuntime.State 
                : new[]
                {
                    "Not Initialized(Stopped)"
                };
            lstState.DataBind();
        }

        protected void lstState_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var item = e.Item;
            if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                (item.FindControl("lblState") as Literal).Text = e.Item.DataItem as String;
        }

        protected void OnCommand(object sender, CommandEventArgs e)
        {
            lblMessage.Visible = false;

            switch(e.CommandName)
            {
                case "Start":
                    {
                        try
                        {
                            var cfg = WebConfigurationManager.OpenWebConfiguration("~");
                            string guidStr = Guid.NewGuid().ToString();

                            if(cfg.AppSettings.Settings["Runtime.Guid"] == null)
                                cfg.AppSettings.Settings.Add("Runtime.Guid", guidStr);
                            else
                                cfg.AppSettings.Settings["Runtime.Guid"].Value = guidStr;
                            cfg.Save(ConfigurationSaveMode.Modified);
                        }
                        catch(Exception ex)
                        {
                            lblMessage.Visible = true;
                            lblMessage.ForeColor = Color.Red;
                            lblMessage.Text = ex.Message;
                        }
                        if(!lblMessage.Visible)
                            Response.Redirect("~/");
                    }
                    break;
                case "Stop":
                    {
                        try
                        {
                            var cfg = WebConfigurationManager.OpenWebConfiguration("~");
                            string guidStr = Guid.Empty.ToString();

                            if(cfg.AppSettings.Settings["Runtime.Guid"] == null)
                                cfg.AppSettings.Settings.Add("Runtime.Guid", guidStr);
                            else
                                cfg.AppSettings.Settings["Runtime.Guid"].Value = guidStr;
                            cfg.Save(ConfigurationSaveMode.Modified);
                        }
                        catch(Exception ex)
                        {
                            lblMessage.Visible = true;
                            lblMessage.ForeColor = Color.Red;
                            lblMessage.Text = ex.Message;
                        }
                        if(!lblMessage.Visible)
                            Response.Redirect("~/");
                    }
                    break;
            }
        }
    }
}